/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.app.memento.Memento;
import fever.app.memento.Originator;
import fever.data.iterator.MementoIterator;
import fever.exception.MementoException;
import fever.utils.Stringifier;

/**
 * Manages a {@link fever.app.memento.Memento} instance list.
 * 
 * @see fever.app.memento.Memento
 * @see fever.app.memento.Originator
 * 
 * @author Romain Ecarnot
 */
class fever.app.memento.CareTaker
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _mementoList : Array;
	private var _iterator : MementoIterator;
	private var _cursor : Number;	private var _subject : Originator;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Read-only. Returns cursor's position in memento iteration. */ 
	public function get index() : Number { return _cursor; }
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param originator (optional) {@link fever.app.memento.Originator} instance
	 */
	public function CareTaker( originator : Originator )
	{
		_subject = originator || null;
		_mementoList = new Array();
	}
	
	/**
	 * Registers a new {@link fever.app.memento.Memento} state.
	 * 
	 * @param a {@link fever.app.memento.Memento} instance
	 */
	public function push( m : Memento ) : Void
	{
		if( _cursor < _mementoList.length - 1)
		{
			 _mementoList.splice( _cursor + 1 );
		}
		
		_mementoList.push( m );
		_iterator = new MementoIterator( _mementoList );
		_cursor = _iterator.getIndex();
	}
	
	/**
	 * Clears memento list.
	 */
	public function clear() : Void
	{
		var memento : Memento = _mementoList[ _cursor ];
		_mementoList = new Array();	
		_cursor = null;
		push( memento );
	}
	
	/**
	 * Returns {@code fever.app.memento.Memento} at index {@code index} 
	 * in caretaler list.
	 */
	public function get( index : Number ) : Memento
	{
		_cursor = index;
		return _mementoList[index];
	}
	
	/**
	 * Undo implementation
	 * 
	 * <p>Returns previous saved state.
	 * 
	 * @throws {@link fever.exception.MementoException} if no state is 
	 * available.
	 */
	public function undo() : Memento
	{
		if( _iterator.hasPrevious() )
		{
			var memento : Memento = _iterator.previous();
			_cursor = _iterator.getIndex();
			return memento;
		}
		else throw new MementoException("No state can be undo" );
	}
	
	/**
	 * Redo implementation
	 * 
	 * <p>Returns next saved state.
	 * 
	 * @throws {@link fever.exception.MementoException} if no state is 
	 * available.
	 */
	public function redo() : Memento
	{
		if( _iterator.hasNext() )
		{
			var memento : Memento = _iterator.next();
			_cursor = _iterator.getIndex();
			return memento;	
		}
		else throw new MementoException("No state can be redo" );
	}
	
	/**
	 * Returns {@code true} if a next state is available in memento list
	 */
	public function hasNext() : Boolean
	{
		return _iterator.hasNext();	
	}
	
	/**
	 * Returns {@code true} if a previous state is available in memento list
	 */
	public function hasPrevious() : Boolean
	{
		return _iterator.hasPrevious();	
	}
	
	/**
	 * Returns the next available memento
	 */
	public function getNext() 
	{
		if( _iterator.hasNext() ) return _mementoList[ _cursor + 1 ];
	}
	
	/**
	 * Returns the previous available memento
	 */
	public function getPrevious()
	{
		if( _iterator.hasPrevious() ) return _mementoList[ _cursor - 1 ];	
	}
	
	/** 
	 * Read-only. Reference to {@code fever.app.memento.Originator} instance.
	 */
	public function getOriginator() : Originator
	{
		return _subject;	
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		var s : String = "";
		var l : Number = _mementoList.length;
		
		for( var i : Number = 0; i < l; i++ )
			s += "\n" + i + " : " +_mementoList[i].toString();	
		
		return Stringifier.parse( this, s );
	} 
}